// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using System;
using System.Collections;
using System.Globalization;

namespace SoapTrace.Core.Formatters
{
	/// <summary>
	/// A collection of web service operations.
	/// </summary>
	public class WebServiceOperationCollection : CollectionBase, ICloneable
	{
		/// <summary>
		/// Creates a new instance of the 
		/// <see cref="WebServiceOperationCollection"/>
		/// </summary>
		public WebServiceOperationCollection( )
		{
		}

		/// <summary>
		/// Adds a web service operation to the collection.
		/// </summary>
		/// <param name="operation">The operation to add.</param>
		public void Add( WebServiceOperation operation )
		{
			InnerList.Add( operation );
		}

		/// <summary>
		/// Gets or sets the operation at the specified index.
		/// </summary>
		public WebServiceOperation this[ int index ]
		{
			get
			{
				return ( WebServiceOperation )InnerList[ index ];
			}

			set
			{
				InnerList[ index ] = value;
			}
		}

		/// <summary>
		/// Finds a web service operation via its name.
		/// </summary>
		/// <param name="operationName">The web service operation's name.</param>
		/// <returns>The Web Service Operation object or null if no 
		/// match could be found.</returns>
		public WebServiceOperation Find( string operationName )
		{
			WebServiceOperation MatchedOperation = null;
	
			for( int i = 0; ( MatchedOperation == null ) && ( i < InnerList.Count ); ++i )
			{
				WebServiceOperation Operation = ( WebServiceOperation )InnerList[ i ];
			
				if( String.Compare( Operation.Name, operationName, true, CultureInfo.InvariantCulture ) == 0 )
				{
					MatchedOperation = Operation;
				}
			}

			return MatchedOperation;
		}

		#region ICloneable Members

		/// <summary>
		/// Creates a deep copy of this object.
		/// </summary>
		/// <returns></returns>
		public object Clone( )
		{
			WebServiceOperationCollection Operations = new WebServiceOperationCollection( );

			foreach( WebServiceOperation Operation in this )
			{
				Operations.Add( ( WebServiceOperation )Operation.Clone( ) );
			}

			return Operations;
		}

		#endregion
		
		/// <summary>
		///   Copies the <see cref='WebServiceOperationCollection'/> values to a one-dimensional <see cref='Array'/> instance at the 
		///    specified index.
		/// </summary>
		/// <param name='array'>The one-dimensional <see cref='Array'/> that is the destination of the values copied from <see cref='WebServiceOperationCollection'/>.</param>
		/// <param name='index'>The index in <paramref name='array'/> where copying begins.</param>
		/// <exception cref='ArgumentException'>
		///   <para><paramref name='array'/> is multidimensional.</para>
		///   <para>-or-</para>
		///   <para>The number of elements in the <see cref='WebServiceOperationCollection'/> is greater than
		///         the available space between <paramref name='arrayIndex'/> and the end of
		///         <paramref name='array'/>.</para>
		/// </exception>
		/// <exception cref='ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>
		/// <exception cref='ArgumentOutOfRangeException'><paramref name='arrayIndex'/> is less than <paramref name='array'/>'s lowbound. </exception>
		/// <seealso cref='Array'/>
		public void CopyTo(WebServiceOperation[] array, int index)
		{
			List.CopyTo(array, index);
		}
		
		/// <summary>
		///   Removes a specific <see cref='WebServiceOperation'/> from the <see cref='WebServiceOperationCollection'/>.
		/// </summary>
		/// <param name='val'>The <see cref='WebServiceOperation'/> to remove from the <see cref='WebServiceOperationCollection'/>.</param>
		/// <exception cref='ArgumentException'><paramref name='val'/> is not found in the Collection.</exception>
		public void Remove(WebServiceOperation val)
		{
			List.Remove(val);
		}	
		
		/// <summary>
		/// <para>Inserts a <see cref='WebServiceOperation'/> into the <see cref='WebServiceOperationCollection'/> at the specified index.</para>
		/// </summary>
		/// <param name='index'>The zero-based index where <paramref name='val'/> should be inserted.</param>
		/// <param name='val'>The <see cref='WebServiceOperation'/> to insert.</param>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='WebServiceOperationCollection.Add'/>
		public void Insert(int index, WebServiceOperation val)
		{
			List.Insert(index, val);
		}
		
		/// <summary>
		///    <para>Returns the index of a <see cref='WebServiceOperation'/> in 
		///       the <see cref='WebServiceOperationCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='WebServiceOperation'/> to locate.</param>
		/// <returns>
		/// <para>The index of the <see cref='WebServiceOperation'/> of <paramref name='val'/> in the 
		/// <see cref='WebServiceOperationCollection'/>, if found; otherwise, -1.</para>
		/// </returns>
		public int IndexOf(WebServiceOperation val)
		{
			return List.IndexOf(val);
		}	
		
		/// <summary>
		/// <para>Gets a value indicating whether the 
		///    <see cref='WebServiceOperationCollection'/> contains the specified <see cref='WebServiceOperation'/>.</para>
		/// </summary>
		/// <param name='val'>The <see cref='WebServiceOperation'/> to locate.</param>
		/// <returns>
		/// <para><see langword='true'/> if the <see cref='WebServiceOperation'/> is contained in the collection; 
		///   otherwise, <see langword='false'/>.</para>
		/// </returns>
		/// <seealso cref='WebServiceOperationCollection.IndexOf'/>
		public bool Contains(WebServiceOperation val)
		{
			return List.Contains(val);
		}	
		
		/// <summary>
		/// <para>Copies the elements of an array to the end of the <see cref='WebServiceOperationCollection'/>.</para>
		/// </summary>
		/// <param name='val'>
		///    An array of type <see cref='WebServiceOperation'/> containing the objects to add to the collection.
		/// </param>
		/// <returns>
		///   <para>None.</para>
		/// </returns>
		/// <seealso cref='WebServiceOperationCollection.Add'/>
		public void AddRange(WebServiceOperation[] val)
		{
			for (int i = 0; i < val.Length; i++) {
				this.Add(val[i]);
			}
		}
		
		/// <summary>
		///     <para>
		///       Adds the contents of another <see cref='WebServiceOperationCollection'/> to the end of the collection.
		///    </para>
		/// </summary>
		/// <param name='val'>
		///    A <see cref='WebServiceOperationCollection'/> containing the objects to add to the collection.
		/// </param>
		/// <returns>
		///   <para>None.</para>
		/// </returns>
		/// <seealso cref='WebServiceOperationCollection.Add'/>
		public void AddRange(WebServiceOperationCollection val)
		{
			for (int i = 0; i < val.Count; i++)
			{
				this.Add(val[i]);
			}
		}
	}
}
